
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace Iridium
{
	public class ggPacketToSend
	{
		ggPacketTypes_Send _type = ggPacketTypes_Send.gg_none;
		MemoryStream act_byt = null;
		BinaryWriter _bw;
		
		public ggPacketTypes_Send packetType
		{
			get { return _type; }
			set { _type = value; }
		}


		public BinaryWriter bw
		{
			get { return _bw; }
			set { _bw = value; }
		}

		public int packetLenght
		{
			get { return (int)act_byt.Length; }
		}
		public ggPacketToSend(NetworkStream ns)
		{
			act_byt = new MemoryStream();
			_bw = new BinaryWriter(act_byt);

		}
		public bool Send(NetworkStream ns)
		{
			try
			{
				BufferedStream bs = new BufferedStream(ns);
				BinaryWriter bw = new BinaryWriter(bs);
				bw.Write((int)_type);

				bw.Write(this.packetLenght);


				bw.Write(act_byt.ToArray());
				

				bw.Flush();
				bs.Flush();




				return true;

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return false;
			}
		}

		
	}
	public class ggPacketToReceive
	{
		ggPacketTypes_Receive _type = ggPacketTypes_Receive.gg_none;
		byte[] act_byt = null;
		
		public ggPacketTypes_Receive packetType
		{
			get { return _type; }
			set { _type = value; }
		}
		public int packetLenght
		{
			get { return act_byt.Length; }
		}
		public byte[] GetActualBytes()
		{

			return act_byt;
		}
		public abstract void FillPacket(BinaryReader reader);

		public bool Receive(NetworkStream ns)
		{
			try
			{

				while (ns.CanRead == false)
				{
				}
				BinaryReader rd = new BinaryReader(ns);


				_type = (ggPacketTypes_Receive)rd.ReadInt32();

				int lenght = rd.ReadInt32();

				act_byt = new byte[lenght + 1];
				for (int i = 1; i <= lenght; i++)
				{
					act_byt[i] = rd.ReadByte();
				}
				FillPacket(new BinaryReader(new MemoryStream(act_byt)));
				return true;
			}
			catch 
			{
				return false;
			}
		}

		
	}
}